package cn.ming.leo.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

public class TimeFormat {

	
	/**
	 * 节假日
	 */
	// TODO 如果跨年了 这些数据都要修改
	private final static Map<String, Boolean> HOLIDAY = new HashMap<String, Boolean>() {
		{
			put("2016-09-15", true);
			put("2016-09-16", true);
			put("2016-09-17", true);
			put("2016-10-01", true);
			put("2016-10-02", true);
			put("2016-10-03", true);
			put("2016-10-04", true);
			put("2016-10-05", true);
			put("2016-10-06", true);
			put("2016-10-07", true);
			put("2016-12-31", true);

			put("2017-01-01", true);
			put("2017-01-02", true);
			put("2017-01-27", true);
			put("2017-01-28", true);
			put("2017-01-29", true);
			put("2017-01-30", true);
			put("2017-01-31", true);
			put("2017-02-01", true);
			put("2017-02-02", true);
			put("2017-04-02", true);
			put("2017-04-03", true);
			put("2017-04-04", true);
			put("2017-04-29", true);
			put("2017-04-30", true);
			put("2017-05-01", true);
			put("2017-05-28", true);
			put("2017-05-29", true);
			put("2017-05-30", true);
			put("2017-10-01", true);
			put("2017-10-02", true);
			put("2017-10-03", true);
			put("2017-10-04", true);
			put("2017-10-05", true);
			put("2017-10-06", true);
			put("2017-10-07", true);
			put("2017-10-08", true);

			put("2017-12-30", true);
			put("2017-12-31", true);

			put("2018-01-01", true);
			put("2018-02-15", true);
			put("2018-02-16", true);
			put("2018-02-17", true);
			put("2018-02-18", true);
			put("2018-02-19", true);
			put("2018-02-20", true);
			put("2018-02-21", true);
			put("2018-04-05", true);
			put("2018-04-06", true);
			put("2018-04-07", true);
			put("2018-04-29", true);
			put("2018-04-30", true);
			put("2018-05-01", true);
			put("2018-06-16", true);
			put("2018-06-17", true);
			put("2018-06-18", true);
			put("2018-09-22", true);
			put("2018-09-23", true);
			put("2018-09-24", true);
			put("2018-10-01", true);
			put("2018-10-02", true);
			put("2018-10-03", true);
			put("2018-10-04", true);
			put("2018-10-05", true);
			put("2018-10-06", true);
			put("2018-10-07", true);
		}
	};

	/**
	 * 正常工作日的加班
	 */
	// TODO 如果跨年了 这些数据都要修改
	private final static Map<String, Boolean> WEEKEN = new HashMap<String, Boolean>() {
		{
			put("2016-09-18", true);
			put("2016-10-08", true);
			put("2016-10-09", true);

			put("2017-01-22", true);
			put("2017-02-04", true);
			put("2017-04-01", true);
			put("2017-05-27", true);
			put("2017-09-30", true);

			put("2018-02-11", true);
			put("2018-02-24", true);
			put("2018-04-08", true);
			put("2018-04-28", true);
			put("2018-09-29", true);
			put("2018-09-30", true);
		}
	};

	/**
	 * 是否是法定节假日和周末双休
	 * 
	 * @param dstr
	 * @return
	 */
	public static boolean isHoliday(String dstr) {
		dstr = dstr.substring(0, 10);
		if (HOLIDAY.containsKey(dstr)) {
			return HOLIDAY.get(dstr);
		}
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date d;
		try {
			d = df.parse(dstr);
		} catch (ParseException e) {
			return false;
		}
		return isWeeken(d);
	}
	public static String hourDiff(String date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date d1 = null;
        Date d2 = new Date();

        try {
            d1 = format.parse(date);

            //毫秒ms
            long diff = d2.getTime() - d1.getTime();

            long diffSeconds = diff / 1000 % 60;
            long diffMinutes = diff / (60 * 1000) % 60;
            long diffHours = diff / (60 * 60 * 1000) % 24;

            System.out.print(diffHours + " 小时, ");
            System.out.print(diffMinutes + " 分钟, ");
            System.out.print(diffSeconds + " 秒.");
            return diffHours + "小时," + diffMinutes + "分钟,  " + diffSeconds + "秒";
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return "";

	}

	/**
	 * 是否是法定节假日和周末双休
	 * 
	 * @param dstr
	 * @return
	 */
	public static boolean isHoliday(Date d) {

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

		String dstr = df.format(d);
		if (HOLIDAY.containsKey(dstr)) {
			return HOLIDAY.get(dstr);
		}

		return isWeeken(d);
	}

	/**
	 * 是否休息日
	 * 
	 * @param d
	 * @return
	 */
	public static boolean isWeeken(Date d) {

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(d);
		int week = calendar.get(Calendar.DAY_OF_WEEK);

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

		if (week == Calendar.SUNDAY || week == Calendar.SATURDAY) {
			// 周末是否调班
			String dstr = df.format(d);
			if (WEEKEN.containsKey(dstr)) {
				return false;
			} else {
				return true;
			}
		} else {
			return false;
		}
	}

	public static String nextDaily(String dstr) {
		dstr = dstr.substring(0, 10);
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date d;
		try {
			d = df.parse(dstr);
		} catch (ParseException e) {
			return null;
		}
		return nextDaily(d);
	}

	/**
	 * 根据日期＋7位随机数 生成编号
	 * 
	 * @return
	 */
	public static String timeNo() {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssS");
		return format.format(new Date())
				+ Math.round(Math.random() * 8999999 + 1000000);
	}

	/**
	 * 根据日期＋5位随机数 生成融宝的用户ID
	 * 
	 * @return
	 */
	public static String realpalTimeNo() {
		SimpleDateFormat format = new SimpleDateFormat("MMddHHmmssS");
		return format.format(new Date())
				+ Math.round(Math.random() * 8999999 + 1000000);
	}

	/**
	 * 返回当前＋给定毫秒值 的时间
	 * 
	 * @param to
	 * @return
	 */
	public static String nowTo(long to) {
		long millis = Calendar.getInstance().getTimeInMillis();
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(
				millis + to));
	}

	/**
	 * 返回指定格式化的时间
	 * 
	 * @param f
	 * @return
	 */
	public static String now(String f) {
		return new SimpleDateFormat(f).format(new Date());
	}

	/**
	 * 返回 yyyy-MM-dd HH:mm:ss 格式的 当前时间
	 * 
	 * @return
	 */
	public static String nowTime() {
		return now("yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 返回 yyyy-MM-dd 格式的时间
	 * 
	 * @return
	 */
	public static String nowDate() {
		return now("yyyy-MM-dd");
	}

	public static String nowMonth() {
		return now("yyyy-MM");
	}

	public static String nowYear() {
		return now("yyyy");
	}

	/**
	 * 返回几天后的时间
	 * 
	 * @param n
	 * @return
	 */
	public static String getNDay(int n) {
		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		c.setTime(new Date());
		c.add(Calendar.DATE, n);
		return df.format(c.getTime());
	}

	/**
	 * 几天后的时间
	 * 
	 * @param n
	 * @return
	 */
	public static Date getNDate(int n) {
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.DATE, n);
		return c.getTime();
	}

	/**
	 * 返回几天后的时间
	 * 
	 * @param n
	 * @return
	 */
	public static String getNDay(int n, String pattern) {
		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat(pattern);
		c.setTime(new Date());
		c.add(Calendar.DATE, n);
		return df.format(c.getTime());
	}

	/**
	 * 返回几天前的时间
	 * 
	 * @param n
	 * @return
	 */
	public static String getBeforeDay(int n, String pattern) {
		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat(pattern);
		c.setTime(new Date());
		c.set(Calendar.DATE, c.get(Calendar.DATE) - n);
		return df.format(c.getTime());
	}

	/**
	 * 获取某天之前（-）、之后（+）日期
	 * 
	 * @param now
	 * @param n
	 * @return
	 */
	public static String getNDay(String now, int n) {
		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			c.setTime(df.parse(now));
		} catch (ParseException e) {
			c.setTime(new Date());
		}
		c.add(Calendar.DATE, n);
		return df.format(c.getTime());
	}

	/**
	 * 获取某月之前（-）、之后（+）日期
	 * 
	 * @param now
	 * @param n
	 * @return
	 */
	public static String getNMonth(String now, int n) {
		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("yyyy-MM");
		try {
			c.setTime(df.parse(now));
		} catch (ParseException e) {
			c.setTime(new Date());
		}
		c.add(Calendar.MONTH, n);
		return df.format(c.getTime());
	}

	/**
	 * 获取某天之前（-）、之后（+）日期
	 * 
	 * @param now
	 * @param n
	 * @return
	 */
	public static String getNTime(String now, int n) {
		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			c.setTime(df.parse(now));
		} catch (ParseException e) {
			c.setTime(new Date());
		}
		c.add(Calendar.DATE, n);
		return df.format(c.getTime());
	}

	/**
	 * 获取某天之前（-）、之后（+）日期
	 * 
	 * @param now
	 * @param n
	 * @return
	 */
	public static String getNTime1(String now, int n) {
		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			c.setTime(df.parse(now));
		} catch (ParseException e) {
			c.setTime(new Date());
		}
		c.add(Calendar.DATE, n);
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		return df.format(c.getTime());
	}

	/**
	 * @author liaozhida
	 * @param smalldate
	 * @param bigdate
	 * @return
	 * @throws ParseException
	 */
	public static int daysBetween(Date smalldate, Date bigdate)
			throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		smalldate = sdf.parse(sdf.format(smalldate));
		bigdate = sdf.parse(sdf.format(bigdate));

		Calendar cal = Calendar.getInstance();

		cal.setTime(smalldate);
		long timeSm = cal.getTimeInMillis();
		cal.setTime(bigdate);
		long timebig = cal.getTimeInMillis();

		long between_days = (timebig - timeSm) / (1000 * 3600 * 24);
		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 返回最近的下一个工作日
	 * 
	 * @param now
	 * @param num
	 * @return
	 */
	public static String nextDaily(Date now) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(now);
		calendar.add(Calendar.DATE, 1);
		while (true) {
			if (!isHoliday(calendar.getTime())) {
				break;
			}
			calendar.add(Calendar.DATE, 1);
		}
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(calendar.getTime());
	}

	/**
	 * @param smallDate
	 * @param bigDate
	 * @param pattern
	 * @return
	 * @throws ParseException
	 */
	public static int strDaysBetween(String smallDate, String bigDate,
			String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		Date sdate;
		Date bdate;
		try {
			sdate = sdf.parse(smallDate);
			bdate = sdf.parse(bigDate);
		} catch (ParseException e) {
			return 0;
		}

		Calendar cal = Calendar.getInstance();
		cal.setTime(sdate);
		long timeSm = cal.getTimeInMillis();
		cal.setTime(bdate);
		long timebig = cal.getTimeInMillis();

		long between_days = (timebig - timeSm) / (1000 * 3600 * 24);
		return Integer.parseInt(String.valueOf(between_days));
	}

	public static String getNSecondTime(int n) {
		Calendar c = Calendar.getInstance();
		String time = nowTime();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			c.setTime(df.parse(time));
		} catch (ParseException e) {
			c.setTime(new Date());
		}
		c.add(Calendar.SECOND, n);
		return df.format(c.getTime());
	}

	/**
	 * 返回两个时间段相差的月数
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 * @throws ParseException
	 */
	public static int getMonthleoace(String date1, String date2)
			throws ParseException {

		int result = 0;

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();

		c1.setTime(sdf.parse(date1));
		c2.setTime(sdf.parse(date2));

		result = (c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR)) * 12;
		result = result + c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);

		return result == 0 ? 1 : Math.abs(result + 1);

	}

	public static String[] getStartAndEnd(String date) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String[] dates = new String[2];

		Calendar c = Calendar.getInstance();
		try {
			c.setTime(df.parse(date));
		} catch (ParseException e) {
			return dates;
		}
		c.add(Calendar.MONTH, 0);
		c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		dates[0] = format.format(c.getTime());
		// 获取当前月最后一天
		c.add(Calendar.MONTH, 1);
		// c.set(Calendar.DAY_OF_MONTH,
		// c.getActualMaximum(Calendar.DAY_OF_MONTH));
		c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		dates[1] = format.format(c.getTime());
		return dates;
	}

	/**
	 * 是否交易所的休息日
	 * 
	 * @param d
	 * @return
	 */
	public static boolean isBourseWeeken(Date d) {

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(d);
		int week = calendar.get(Calendar.DAY_OF_WEEK);

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

		if (week == Calendar.SUNDAY || week == Calendar.SATURDAY) {
			// 周末是否调班
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 返回最近的下一个工作日
	 * 
	 * @param now
	 * @param num
	 * @return
	 */
	public static String nextBourseDaily(String now) {
		return nextDaily(now);
	}

	/**
	 * 是否是法定节假日和周末双休
	 * 
	 * @param dstr
	 * @return
	 */
	public static boolean isBourseHoliday(Date d) {

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

		String dstr = df.format(d);
		if (HOLIDAY.containsKey(dstr)) {
			return HOLIDAY.get(dstr);
		}

		return isBourseWeeken(d);
	}

	/**
	 * 返回次日凌晨0点的时间
	 * 
	 * @return
	 */
	public static long getSecondDateZoreTime() {
		Calendar todayEnd = Calendar.getInstance();
		todayEnd.set(Calendar.HOUR_OF_DAY, 23);
		todayEnd.set(Calendar.MINUTE, 59);
		todayEnd.set(Calendar.SECOND, 59);
		return todayEnd.getTime().getTime();
	}

	/**
	 * 日期格式字符串转换成时间戳
	 * 
	 * @param date
	 *            字符串日期
	 * @param format
	 *            如：yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static double date2TimeStamp(String date_str, String format) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.parse(date_str).getTime() / 1000;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 返回当天的某个时间点 字符串
	 * 
	 * @return
	 */
	public static String designatedNowDate(Integer hour, Integer minute,
			Integer second) {

		Calendar todayEnd = Calendar.getInstance();
		todayEnd.set(Calendar.HOUR_OF_DAY, hour);
		todayEnd.set(Calendar.MINUTE, minute);
		todayEnd.set(Calendar.SECOND, second);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = sdf.format(todayEnd.getTime());

		return dateStr;
	}

	/**
	 * 返回小时后的时间
	 * 
	 * @param n
	 * @return
	 */
	public static String getNHour(String now, int n) {

		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH");
		try {
			c.setTime(df.parse(now));
		} catch (ParseException e) {
			c.setTime(new Date());
		}
		c.add(Calendar.HOUR, n);
		return df.format(c.getTime());
	}

	/**
	 * 返回n小时后的时间
	 * 
	 * @param n
	 * @return
	 */
	public static String getNHourTime(String now, int n) {

		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			c.setTime(df.parse(now));
		} catch (ParseException e) {
			c.setTime(new Date());
		}
		c.add(Calendar.HOUR, n);
		return df.format(c.getTime());
	}

	public static int strHoursBetween(String smallDate, String bigDate,
			String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		Date sdate;
		Date bdate;
		try {
			sdate = sdf.parse(smallDate);
			bdate = sdf.parse(bigDate);
		} catch (ParseException e) {
			return 0;
		}

		Calendar cal = Calendar.getInstance();
		cal.setTime(sdate);
		long timeSm = cal.getTimeInMillis();
		cal.setTime(bdate);
		long timebig = cal.getTimeInMillis();

		long between_days = (timebig - timeSm) / (1000 * 3600);
		return Integer.parseInt(String.valueOf(between_days));
	}

	public static String[] getStartAndEndTwityFity(String date) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String[] dates = new String[2];

		Calendar c = Calendar.getInstance();
		try {
			c.setTime(df.parse(date));
		} catch (ParseException e) {
			return dates;
		}
		c.add(Calendar.MONTH, 0);
		c.set(Calendar.DAY_OF_MONTH, 25);// 设置为1号,当前日期既为本月第一天
		dates[1] = format.format(c.getTime());
		// 获取当前月最后一天
		c.add(Calendar.MONTH, -1);
		// c.set(Calendar.DAY_OF_MONTH,
		// c.getActualMaximum(Calendar.DAY_OF_MONTH));
		c.set(Calendar.DAY_OF_MONTH, 25);// 设置为1号,当前日期既为本月第一天
		dates[0] = format.format(c.getTime());
		return dates;
	}

	/**
	 * 获取距离今天还剩下多少时间
	 * 
	 * @return
	 */
	public static long getSecondsToNowDay() {
		long interval = (getTimesnight().getTime() - new Date().getTime()) / 1000;
		if (interval <= 0) {
			interval = 1;
		}
		return interval;
	}

	private static Date getTimesnight() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 
	 * 返回两个时间段相差的天数
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 * @throws ParseException
	 */
	public static int getDayleoace(String date1, String date2)
			throws ParseException {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date one;
		Date two;
		long days = 0;
		try {
			one = df.parse(date1);
			two = df.parse(date2);
			long time1 = one.getTime();
			long time2 = two.getTime();
			long diff;
			if (time1 < time2) {
				diff = time2 - time1;
			} else {
				diff = time1 - time2;
			}
			days = diff / (1000 * 60 * 60 * 24);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return (int) days;
	}

	/**
	 * 计算出两个时间的小时差
	 * 
	 * @param beginDate
	 * @param endDate
	 * @return
	 * @throws ParseException
	 */
	public static int getDateHourDiff(String beginDate, String endDate) {
		try {
			SimpleDateFormat simpleFormat = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");

			long from = simpleFormat.parse(beginDate).getTime();
			long to = simpleFormat.parse(endDate).getTime();
			int hours = (int) ((to - from) / (1000 * 60 * 60));

			return Math.abs(hours);
		} catch (ParseException e) {
			return 0;
		}
	}

	/**
	 * 时间字符串转化成指定格式
	 * 
	 * @param date
	 *            时间字符串 ： 2010-10-01 04:02:03
	 * @param pattern
	 *            要转化成的格式 ： yyyy-MM-dd
	 * @return
	 */
	public static String stringFormatDate(String date, String pattern) {

		try {
			Date temp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date);
			return new SimpleDateFormat(pattern).format(temp);
		} catch (ParseException e) {
			e.printStackTrace();
			return date;
		}
	}

	/**
	 * 借款协议到期日计算
	 * 
	 * @author zarek.wu
	 * @return
	 */
	public static String afterSomeDay(String now) throws Exception {
		Calendar calendar = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		calendar.setTime(df.parse(now));
		calendar.add(Calendar.DATE, -1);
		while (true) {
			if (!isHoliday(calendar.getTime())) {
				break;
			}
			calendar.add(Calendar.DATE, -1);
		}
		return df.format(calendar.getTime());
	}

	/**
	 * 获取任意时间的第n月第一天
	 * 
	 * @param repeatDate
	 *            时间
	 * @param n
	 *            月：（-）前，（+）后
	 * @return
	 */
	public static String getMinMonthDate(String repeatDate, int n) {
		SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		try {
			if (StringUtils.isNotBlank(repeatDate)
					&& !"null".equals(repeatDate)) {
				calendar.setTime(dft.parse(repeatDate));
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		calendar.add(Calendar.MONTH, n);
		calendar.set(Calendar.DAY_OF_MONTH,
				calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		return dft.format(calendar.getTime());
	}

	/**
	 * 获取第n个月的最后一天.
	 * 
	 * @param date
	 *            时间
	 * @param n
	 *            月：（-）前，（+）后
	 * @param pattern
	 *            参数格式，如果date 是 2017-01-01 pattern为 yyyy-MM-dd
	 * @return
	 */
	public static String getLastMaxMonthDate(String date, int n, String pattern) {

		SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date temp = new SimpleDateFormat(pattern).parse(date);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(temp);
			calendar.add(Calendar.MONTH, n);
			calendar.set(Calendar.DAY_OF_MONTH,
					calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
			return dft.format(calendar.getTime());
		} catch (ParseException e) {
			return date;
		}
	}

	/**
	 * 仅计算时间格式为hh:mm:ss所对应的秒数
	 * 
	 * @param beginDate
	 * @param endDate
	 * @return
	 */
	public static int calculateSeconds(String time) {

		try {
			SimpleDateFormat simpleFormat = new SimpleDateFormat("HH:mm:ss");

			long from = simpleFormat.parse("00:00:00").getTime();
			long to = simpleFormat.parse(time).getTime();
			int hours = (int) ((to - from) / (1000));

			return Math.abs(hours);
		} catch (ParseException e) {
			return 0;
		}
	}

	public static String getCode() {
		return String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
	}

	public static void main(String[] args) {

	}

	public static String nextYear(Date now) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(now);
		calendar.add(Calendar.YEAR, 1);

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(calendar.getTime());
	}

	public static String nextYearStr(String time) {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = format.parse(time); // Thu Jan 18 00:00:00 CST 2007
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, 1);

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(calendar.getTime());
	}
}
